package com.bolt.convention.exception;


import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.convention.data.code.CommonCode;
import com.bolt.convention.data.code.ServiceCode;

import java.util.ArrayList;
import java.util.List;

/**
 * 验证异常
 *
 * @author xiaoleilu
 */
public class ServiceValidException extends ServiceException {
    private static final long serialVersionUID = 6057602589533840889L;


    private List <ResultMessage.ViolationItem> violationItems = new ArrayList <>();

    public ServiceValidException() {
        super(CommonCode.INVALID_ARGS);
    }

    public ServiceValidException(String message) {
        super(CommonCode.INVALID_ARGS.code(), message);
    }

    public ServiceValidException(String message, Throwable cause) {
        super(CommonCode.INVALID_ARGS.code(), message, cause);
    }

    public ServiceValidException(String code, String message, Throwable cause) {
        super(code, message, cause);
    }

    public ServiceValidException(String code, String message, List <ResultMessage.ViolationItem> violationItems) {
        super(code, message);
        this.violationItems = violationItems;
    }

    public ServiceValidException(List <ResultMessage.ViolationItem> violationItems) {
        super(CommonCode.INVALID_ARGS.code(), CommonCode.INVALID_ARGS.message());
        this.violationItems = violationItems;
    }

    public ServiceValidException(ServiceCode serviceCode) {
        super(serviceCode);
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public void setCode(String code) {
        this.code = code;
    }

    public List <ResultMessage.ViolationItem> getViolationItems() {
        return violationItems;
    }

    public ServiceValidException addViolationItem(String field, String message) {
        if (this.violationItems == null) {
            this.violationItems = new ArrayList <>();
        }
        violationItems.add(Results.buildViolationItem(field, message));
        return this;
    }

    public ServiceValidException(String messageTemplate, Object... params) {
        super(CommonCode.INVALID_ARGS.code(), CommonCode.INVALID_ARGS.message());
    }

    @Override
    public Throwable fillInStackTrace() {
        return this;
    }

    @Override
    public String toString() {
        return "ServiceValidException{" +
                "code='" + code + '\'' +
                ", message='" + super.getMessage() + '\'' +
                ", violationItem=" + violationItems +
                '}';
    }


}
